package com.daocao.ds.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.view.OptionsPickerView;
import com.daocao.ds.R;
import com.daocao.ds.activity.MyMessageActivity;
import com.daocao.ds.adapter.TabTopAdapter;
import com.daocao.ds.constant.UserInfoGlobal;
import com.daocao.ds.event.MainChangePageEvent;
import com.daocao.ds.util.SelectTimeUtil;
import com.daocao.ds.util.Util;
import com.daocao.ds.view.HomeViewPager;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-11-16.
 */

public class MyOrderFragment extends BaseFragment {
    @BindView(R.id.btn_left)
    TextView btnLeft;
    @BindView(R.id.btn_right)
    ImageView btnRight;
    @BindView(R.id.tablayout)
    RecyclerView tablayout;
    @BindView(R.id.pager)
    HomeViewPager pager;
    Unbinder unbinder;
    private TabTopAdapter TabAdapter;
    private String date;
    private OptionsPickerView pickerView;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_my_order;
    }

    @Override
    protected void initView() {
        TabAdapter = new TabTopAdapter(getContext());
        TabAdapter.setOnChooseListener(new TabTopAdapter.OnChooseListener() {
            @Override
            public void onClick(int poi) {
                pager.setCurrentItem(poi);
            }
        });
        tablayout.setLayoutManager(new GridLayoutManager(getContext(), 4));
        tablayout.setAdapter(TabAdapter);
        List<String> tab_titles = new ArrayList<>();
        tab_titles.add("全部");
        tab_titles.add("使用中");
        tab_titles.add("已回款");
        tab_titles.add("已逾期");
        TabAdapter.update(tab_titles);

        Fragment[] fragments = new Fragment[4];
        for (int i = 0; i < 4; i++) {
            MyItemOrderFragment fragment = new MyItemOrderFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("id", i);
            fragment.setArguments(bundle);
            fragments[i] = fragment;
        }

        pager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {

            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }
        });

        Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(new Date().getTime());
        int year = mCalendar.get(Calendar.YEAR);
        int month = mCalendar.get(Calendar.MONTH);
        btnLeft.setText(year + "/" + (month + 1));
        date = year + "" + (month + 1);
        UserInfoGlobal.date = date;
        pickerView = SelectTimeUtil.getTimeView(getContext(), new SelectTimeUtil.DialogDefaultClickListener() {
            @Override
            public void confirm(String year, String month) {
                date = year + month;
                UserInfoGlobal.date = date;
                btnLeft.setText(year + "/" + month);
                EventBus.getDefault().post(new MainChangePageEvent());
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.btn_left, R.id.btn_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_left:
                pickerView.show();
                break;
            case R.id.btn_right:
                Util.startActivity(getContext(), MyMessageActivity.class);
                break;
        }
    }
}
